package com.iengoo.inductionms.service.impl;

import com.iengoo.inductionms.entity.Employee;
import com.iengoo.inductionms.entity.User;
import com.iengoo.inductionms.enums.ExceptionMessage;
import com.iengoo.inductionms.exception.LoginException;
import com.iengoo.inductionms.mapper.EmployeeMapper;
import com.iengoo.inductionms.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public User Login(Employee employee) {
        User user = new User();
        //登陆前判断账号是否存在
        Employee byUsername = employeeMapper.findByUsername(employee.getUsername());
        if (byUsername == null){
            throw new LoginException(String.format(ExceptionMessage.USERNAME_IS_NOT_EXIST.getDesc()));
        }
        if (byUsername != null) {
            if (!byUsername.getPassword().equals(employee.getPassword())) {
                throw new LoginException(String.format(ExceptionMessage.PASSWORD_IS_NOT_CORRECT.getDesc()));
            } else {
                //判断账号是否被激活，即入职状态为6为激活
//                String type = byUsername.getType();
//                if (!type.equals("6")){
//                    throw new LoginException(String.format(ExceptionMessage.USER_NOT_ENABLED.getDesc()));
//
//                }else {
//                    user.setUsername(byUsername.getUsername());
//                    user.setPassword(null);
//                    user.setDepartment(byUsername.getDepartment());
//                    user.setRole(employee.getRole());
//                }

                user.setId(byUsername.getId());
                user.setUsername(byUsername.getUsername());
                user.setPassword(null);
                user.setDepartment(byUsername.getDepartment());
                user.setRole(byUsername.getRole());
            }
        }

        return user;
    }

}
